package com.hzgj.demo.a;

import com.hzgj.demo.b.BService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/8/29.
 */
public class Bootstrap {

    private Logger logger = LoggerFactory.getLogger(getClass());

    Random random
            = new Random();

    public static void main(String[] args) {
        new Bootstrap().invoke(args);
    }

    public void invoke(String[] args) {
        BService bService = new ClassPathXmlApplicationContext("classpath:config.xml").getBean(BService.class);
        int count = 3;
        if (args.length != 0) {
            try {
                count = Integer.parseInt(args[0]);
            } catch (Exception e) {

            }
        }
        logger.info("start client with " + count + " threads");

        for (int i = 0; i < count; i++) {
            new Thread(() -> {

                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(random.nextInt(3));
                        long start = System.currentTimeMillis();
                        bService.getServiceFromCF();
                        logger.info("spend " + (start - System.currentTimeMillis()));
                    } catch (Exception e) {
                        logger.info("{}", e);
                    }
                }
            }).start();
        }
    }
}
